var app = getApp();
Page({
  data: {
    selectedIndex:0,
    index:0
  },
  onLoad: function (options) {
    let that = this
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight,
    });
    that.setData({
      balance: wx.getStorageSync("balance"),
      productIdAndNumList: options.productIdAndNumList,
      addressId: options.addressId,
      type: options.type,
      couponId: options.couponId,
      remark: options.remark,
      banlance: options.banlance,
      groupShoppingId: options.groupShoppingId,
      giftCardWay: options.giftCardWay,
      price1: options.price1,
      shoppingCart: options.shoppingCart,
      myOrder: options.myOrder,
      orderId: options.orderId,
      orderid: options.orderId,
      communityGroup: options.communityGroup,
      communityheadid: options.communityheadid,
      typeShow: options.typeShow,
      promotionId:options.promotionId,
      deduct: options.deduct,
      jinHeCardId:options.jinHeCardId,
      jinHeCardNo:options.jinHeCardNo,
      jinHeCardMoney:options.jinHeCardMoney,
    })

    if(!that.data.jinHeCardId){
      that.setData({
        index: 0,
        selectedIndex: 0
      })
    }else{
      that.setData({
        index: 2,
        selectedIndex: 2
      })
    }
  },
  
  onShow:function(){
    var that = this;
  },
  onReady: function () {
    var that = this;
  },
  navBack: function () {
    var that = this;
    wx.navigateBack({

    })
  },
  // 单个选中
  change: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    if (that.data.selectedIndex != index) {
      that.setData({
        index: index,
        selectedIndex: index
      })
    }
    if(index==2){//金和一卡通支付，根据手机号查询用户一卡通卡包信息，供用户选择支付卡号
      wx.navigateTo({
        url: '/pages/jinHeYiKaTong/jinHeYiKaTong?productIdAndNumList='+that.data.productIdAndNumList+'&addressId='+that.data.addressId+'&type='+that.data.type
        +'&couponId='+that.data.couponId+'&remark='+that.data.remark+'&banlance='+that.data.banlance+'&groupShoppingId='+that.data.groupShoppingId+'&giftCardWay='+that.data.giftCardWay
        +'&price1='+that.data.price1+'&shoppingCart='+that.data.shoppingCart+'&myOrder='+that.data.myOrder+'&orderId='+that.data.orderId+'&orderid='+that.data.orderId
        +'&communityGroup='+that.data.communityGroup+'&communityheadid='+that.data.communityheadid+'&typeShow='+that.data.typeShow+'&promotionId='+that.data.promotionId+'&deduct='+that.data.deduct
      })
    }else{
      return false;
    }
  },
  immeBuy:function(e){
    var  that = this;
    if(that.data.selectedIndex==2&&!that.data.jinHeCardId){
      wx.showToast({
        title: "请选择金和智慧通卡片",
        icon: 'none'
      })
      return false;
    }
    if (that.data.myOrder != 1) {
      if (that.data.selectedIndex == 1) {
        var payChannel = 5003;
      } else if(that.data.selectedIndex == 0) {
        var payChannel = 5005;
      }else if(that.data.selectedIndex == 2) {
        var payChannel = 5008;
      }
      console.info(that.data.list);
      wx.showLoading({
        title: '正在提交...',
        icon: 'loading',
      })
      if (that.data.communityheadid == undefined || that.data.communityheadid =='undefined') {
        var communityheadid=0;
      } else {
        var communityheadid = that.data.communityheadid;
      }
      console.info(that.data.shoppingCart);
      if (that.data.shoppingCart == 'undefined' || that.data.shoppingCart == undefined) {
        var shoppingCart = false;
      } else {
        var shoppingCart = that.data.shoppingCart
      }
      if (that.data.typeShow == 0) {
        if (that.data.type == 6) {
          var type = that.data.type
        } else if (that.data.type == 5) {
          var type = that.data.type
        } else if (that.data.type == 3) {
          var type = that.data.type
        } else {
          var type = 1
        }
       
      } else {
        var type = that.data.type;
      }
      
      wx.request({
        url: app.globalData.url + "submitOrder",
        method: "GET",
        data: {
          userId: wx.getStorageSync("userId"),
          productIdAndNumList: that.data.productIdAndNumList,
          addressId: that.data.addressId,
          type: type,
          couponId: that.data.couponId,
          remark: that.data.remark,
          banlance: that.data.banlance,
          groupShoppingId: that.data.groupShoppingId,
          giftCardWay: that.data.giftCardWay,
          payChannel: payChannel,
          jinHeCardId: that.data.jinHeCardId,
          shoppingCart: shoppingCart,
          communityHeadId: communityheadid,
          promotionId:that.data.promotionId,
          deduct: that.data.deduct
        },
        success: function (res) {
          var data = res.data.data;
          console.log(res.data);
         // wx.setStorageSync("balance", data.balance)
          if (res.data.result == 0) {
            wx.hideLoading();
            wx.showToast({
              title: res.data.errmsg,
              icon: 'none'
            })

            return;
          }
          that.setData({
            orderid: data.order.orderId,
          })
          if (data.order.orderStatus != 7001 && data.order.orderStatus != 7005) {
            // that.modal_s();
            that.setData({
              userId: wx.getStorageSync("userId"),
              orderId: data.order.orderId,
            })
            wx.request({
              url: app.globalData.url + 'getOrderWeixinPrepayId',
              data: {
                userId: wx.getStorageSync("userId"),
                orderId: data.order.orderId,
                orderNumber: data.order.orderNumber,
              },
              method: "POST",
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                var resultData = res.data;
                var hisId = resultData.hisId;
                if (res.data.result != 1 && res.data.result != 9) {
                  wx.showToast({
                    title: "数据异常，请稍后重试",
                    icon: 'none'
                  })
                  return;
                }
                wx.requestPayment({
                  'timeStamp': resultData.timeStamp,
                  'nonceStr': resultData.nonceString,
                  'package': resultData.packageStr,
                  'signType': resultData.signType,
                  'paySign': resultData.sign,
                  //支付成功
                  success: function (res) {
                    if ("requestPayment:ok" === res.errMsg) {
                      wx.showToast({
                        title: '支付成功',
                        icon: 'success',
                        duration: 2000,
                        success: function () {
                          
                          if (that.data.type == 2 && that.data.typeShow !=0) {
                            wx.navigateTo({
                              url: '/pages/assembleDetail/assembleDetail?orderId=' + that.data.orderid + '&back=1'
                            })
                          } else if (that.data.type == 2 && that.data.typeShow == 0) {
                            wx.navigateTo({
                              url: '/pages/paySeccess/paySeccess?orderId=' + that.data.orderid + '&back=1'
                            })
                          } else {
                            console.info(that.data.lipinka);
                            if (that.data.lipinka != 3) {
                              wx.navigateTo({
                                url: '/pages/paySeccess/paySeccess?orderId=' + that.data.orderid + '&back=1'
                              })
                            } else {
                              wx.navigateTo({
                                url: '/pages/paySeccess/paySeccess?orderId=' + that.data.orderid + '&back=1'
                              })
                            }

                          }

                        }
                      })
                    }
                  },
                  //支付或者取消失败
                  fail: function (res) {
                    if ("requestPayment:fail cancel" === res.errMsg) {
                      wx.showToast({
                        title: '取消支付',
                        icon: 'none',
                        duration: 2000,
                        success: function () {
                          if (that.data.type == 2) {
                            wx.navigateBack({
                            });
                            wx.request({
                              url: app.globalData.url + 'delOrder',
                              method: 'GET',
                              data: {
                                orderId: that.data.orderid,
                                userId: wx.getStorageSync("userId")
                              },
                              success: function (res) {
                                wx.navigateBack({ // 返回首页
                                  delta: 1,
                                })
                              }
                            });
                          } else {
                            wx.showToast({
                              title: "取消支付",
                              icon: 'none'
                            })
                            wx.navigateBack({
                              delta: 1,
                            });
                            // setTimeout(function () {
                            //   wx.navigateBack({
                            //     delta: 1,
                            //   });
                            // }, 2000) //延迟时间
                          }

                        }
                      })
                    } else {

                      wx.showToast({
                        title: "支付失败",
                        icon: 'none'
                      })
                    }
                  },
                })
              }
            })
          } else if (res.data.result == 1 && res.data.data.order.orderStatus == 7001) {
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 2000,
              success: function () {
                console.log('haha');
                if (that.data.type == 2 && that.data.typeShow != 0) {
                  wx.navigateTo({
                    url: '/pages/assembleDetail/assembleDetail?orderId=' + that.data.orderid + '&back=1'
                  })
                } else if (that.data.type == 2 && that.data.typeShow == 0) {
                  wx.navigateTo({
                    url: '/pages/paySeccess/paySeccess?orderId=' + that.data.orderid + '&back=1'
                  })
                } else {
                  console.info(that.data.lipinka);
                  if (that.data.lipinka != 3) {
                    wx.navigateTo({
                      url: '/pages/paySeccess/paySeccess?orderId=' + that.data.orderid + '&back=1'
                    })
                  } else {
                    wx.navigateTo({
                      url: '/pages/giftCardBuySuccess/giftCardBuySuccess?orderId=' + that.data.orderid + '&back=1'
                    })
                  }

                }
                // wx.navigateTo({
                //   url: '/pages/paySeccess/paySeccess?orderId=' + that.data.orderid + '&back=1'
                // })
                // setTimeout(function () {
                //   wx.navigateBack({
                //     delta: 2,
                //   });
                // }, 2000) //延迟时间
              }
            })
          } else if (res.data.result == 1 && res.data.data.order.orderStatus == 7005){
            wx.navigateTo({
              url: '/pages/giftCardBuySuccess/giftCardBuySuccess?orderId=' + that.data.orderid + '&back=1'
            })
          }
        }
      });
    } else {
      if (that.data.selectedIndex ==0) {
        wx.request({
          url: app.globalData.url + 'balancePay',
          method: 'GET',
          data: {
            userId: wx.getStorageSync("userId"),
            orderId: that.data.orderId,
          },
          success: function (res) {
            if (res.data.result == 1) {
              if (that.data.type == 2 && that.data.typeShow != 0) {
                wx.navigateTo({
                  url: '/pages/assembleDetail/assembleDetail?orderId=' + e.target.dataset.ordid + '&back=1'
                })
              } else if (that.data.type == 2 && that.data.typeShow == 0) {
                wx.navigateTo({
                  url: '/pages/paySeccess/paySeccess?orderId=' + e.target.dataset.ordid + '&back=1'
                })
              } else {
                console.info(that.data.lipinka);
                if (that.data.lipinka != 3) {
                  wx.navigateTo({
                    url: '/pages/paySeccess/paySeccess?orderId=' + e.target.dataset.ordid + '&back=1'
                  })
                } else {
                  wx.navigateTo({
                    url: '/pages/giftCardBuySuccess/giftCardBuySuccess?orderId=' + e.target.dataset.ordid + '&back=1'
                  })
                }

              }

            } else {
              wx.showToast({
                title: res.data.errmsg,
                icon: 'none'
              })
            }
          }
        });
      } else {
        wx.request({
          url: app.globalData.url + 'getOrderWeixinPrepayId',
          data: {
            userId: wx.getStorageSync("userId"),
            orderId: that.data.orderId,
            orderNumber:0,
          },
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            var resultData = res.data;
            var hisId = resultData.hisId;
            if (res.data.result != 1 && res.data.result != 9) {
              wx.showToast({
                title: "数据异常，请稍后重试",
                icon: 'none'
              })
              return;
            }
            wx.requestPayment({
              'timeStamp': resultData.timeStamp,
              'nonceStr': resultData.nonceString,
              'package': resultData.packageStr,
              'signType': resultData.signType,
              'paySign': resultData.sign,
              //支付成功
              success: function (res) {
                console,log(11111111111)
              },
              //支付或者取消失败
              fail: function (res) {
                if ("requestPayment:fail cancel" === res.errMsg) {

                  wx.showToast({
                    title: "取消支付",
                    icon: 'none'
                  })

                } else {
                  wx.showToast({
                    title: "支付失败",
                    icon: 'none'
                  })

                }

              }
            })
          }
        })
      }
      
    }
    
  },
})


